package or.exa.cangguan.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Page;
import or.exa.cangguan.common.Result;
import or.exa.cangguan.entity.dto.GoodsDto;
import or.exa.cangguan.entity.vo.InStoreVo;
import or.exa.cangguan.service.InStoreService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api(tags = "入库管理")
public class InStoreController {
    @Autowired
    private InStoreService inStoreService;




    @PostMapping("instore/add")
    @ApiOperation("新增入库")
    public Result addInStore(@RequestBody InStoreVo inStoreVo) {
        // 参数校验
        if (inStoreVo.getGoodsList() == null || inStoreVo.getGoodsList().isEmpty()) {
            return Result.ok("商品列表不能为空");
        }
        if (inStoreVo.getInPlanTime() == null) {
            return Result.ok("计划入库时间不能为空");
        }
        if (StringUtils.isBlank(inStoreVo.getGoodsSource())) {
            return Result.ok("货物来源不能为空");
        }
        if (StringUtils.isBlank(inStoreVo.getStoreName())) {
            return Result.ok("仓库不能为空");
        }

        int i = inStoreService.addInStore(inStoreVo);
        return i > 0 ? Result.ok("入库单创建成功") : Result.ok("入库单创建失败");
    }


    //更新入库单
    @PostMapping("instore/update")
    @ApiOperation("更新入库")
    public Result updateInStore(@RequestBody InStoreVo inStoreVo) {

        int i = inStoreService.updateInStore(inStoreVo);

        return i> 0 ? Result.ok("入库单更新成功") : Result.ok("入库单更新失败");
    }


    //查询入库单
    @PostMapping("instore/search")
    @ApiOperation("查询入库")
    public Result queryInStore(@RequestBody InStoreVo inStoreVo) {
        Page page = inStoreService.searchInStore(inStoreVo);

        return Result.ok(page);

    }


    //查询入库单商品
    @PostMapping("instore/searchGoods/{id}")
    @ApiOperation("查询入库单商品")
    public Result queryInStoreGoods(@PathVariable Integer id) {
        List<GoodsDto> goodsDtoList = inStoreService.searchInStoreGoods(Integer.valueOf(id));
        return Result.ok(goodsDtoList);
    }

    //删除入库单
    @ApiOperation("删除入库单")
    @PostMapping("instore/delete/{id}")
    public Result deleteInStore(@PathVariable Integer id) {

        int i = inStoreService.deleteInStore(id);
        log.info("-------------"+i);
        return i > 0 ? Result.ok("删除成功") : Result.ok("删除失败");
    }



    //更新状态
    @ApiOperation("更新状态")
    @PostMapping("instore/updateStatus")
    public Result updateStatus(@RequestBody InStoreVo inStoreVo) {

        int i = inStoreService.updateStatus(inStoreVo.getId(),inStoreVo.getInStatus(),inStoreVo.getInPlanTime());
        return  i > 0 ? Result.ok("更新成功") : Result.ok("更新失败");
    }

}
